package com.chenwut.threadDemo.threadsafe2;

/**
 * @author ChenPi
 * @since 2024/09/18 13:50
 */
public class MyRunable implements Runnable {
    int ticket = 0;

    @Override
    public void run() {
        while (true) {
            if (method()) {
                break;
            }
        }
    }

    private synchronized boolean method() {
        if (ticket < 100) {
            try {
                Thread.sleep(10);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            ticket++;
            System.out.println(Thread.currentThread().getName() + "正在卖第" + ticket + "张票");
        } else {
            return true;
        }
        return false;
    }
}
